<?php

defined('BASEPATH') or die('forbidden');

class Module_Controller Extends Controller {

	
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        require_once(CONFIGPATH . 'inc.php');
        
    }

    function index() {

        $this->view->display("activity/module/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        
        //print_r($colsinfo);
        
        $rows = array();       

        $sql = "SELECT COUNT(id) AS num FROM act_module WHERE {$where}";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $selectSql="SELECT {$colsinfo} FROM act_module WHERE {$where}  ORDER BY {$sortname} {$sortorder}  LIMIT {$offset},{$rp}"; 
        $this->db->query($selectSql);      
        while ($n = $this->db->get_array('default', MYSQL_ASSOC)) {
        	//print_r($n);
            $rows[] = array('id' => $n['tableenum'], 'cell' => array_values($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $selectSql="select id from act_module where tableenum = '$rowdata[tableenum]'";   
            //echo $selectSql;   
			$cRow=$this->db->one($selectSql);
			if ($cRow['id']>0)
			{
				$message = " add new module error tableenum:".$rowdata['tableenum']."  already exists !";
				//echo $message;
				echo "<script>alert('".$message."');</script>";
				cp_tips('error', $rowdata['tableenum']);
			}
			else 
			{
				//print_r($rowdata);
				if(false == $this->db->insert('act_module', $rowdata, true))
				{
					cp_tips('error', $rowdata['tableenum']);
				}
				$id = $this->db->insert_id();
				
				//组名策略名要相应的更新
				foreach ($this->groupList as $key => $value)
				{
					unset($data);
					$data['groupid'] = $key;
					$data['groupname'] = $value;
					$data['strategyid'] = 1;	//默认是1号策略
					$data['tablename'] = $rowdata['tablename'];
					$data['des'] = $rowdata['des'];
					$data['tableenum'] = $rowdata['tableenum'];
					//print_r($data);
					if(false == $this->db->insert('act_group_strategy', $data))
					{
						cp_tips('error', $data['groupname']);
					}
				}
				
            	insert_log(translate('MENU_ACTIVITY_MODULE'),translate('ADD'), $rowdata['modulename']);
            	cp_tips('success', $rowdata['modulename']);
			}
        }
        $this->view->display("activity/module/add");
    }

    function edit($id) {
        if (!$id)
        {
            error(translate('C_INVALID_PARAM_ID', $id));
        }
        $row = $this->db->one("SELECT id FROM act_module WHERE tableenum={$id}");
        if (!is_array($row))
        {
            error(translate('C_INVALID_PARAM_ID', $id));
        }
        if (is_submit()) {	//提交按钮
            $rowdata = $_POST['row'];
            //print_r($rowdata);
            //echo " id = $id ";
            $sql = " update act_module set tableenum = $rowdata[tableenum], des='$rowdata[des]', 
            		tablename='$rowdata[tablename]' where tableenum = $id ";
            //echo " sql = $sql ";
            //$result = $this->db->update('act_module', $rowdata,array("tableenum"=>$id));
            //echo "result = $result";
            $result = $this->db->execute($sql);
            if (false != $result) {
                //$rowdata['id'] = $row['id'];
                insert_log(translate('MENU_ACTIVITY_MODULE'),translate('EDIT'), $rowdata['tableenum']);
                //print_r($rowdata);
                //更新下模块策略名
                $data['des']=$rowdata['des'];
                $data['tableenum']=$rowdata['tableenum'];
                $data['tablename']=$rowdata['tablename'];
                $this->db->update('act_group_strategy', $data, array('tableenum' => $id));
                echo "success";
                cp_tips('success', $id);
            } else {
               // error(translate('C_TIPS_ERROR_TEXT'));
               echo " update false sql tableenum = $id";
            }
        }else 
        {
			//不是提交选项
	        $row=$this->db->one("select * from act_module where tableenum=$id ");
	        
	        //把系数传过去
	        $sql = "select * from $row[tablename] ";
	        //echo " sql:$sql ";
	        
	        $this->db->query($sql);
			$xishu = array();
			
			$count = 0;
	    	while ($n = $this->db->get_array('default', MYSQL_BOTH)) {
	        	//echo "id = ".$n['id'];
	        	//print_r($n);
	        	
	            $xishu[$count] =$n;
	            ++$count;
	        }
	        //var_dump($xishu);
	        
	        $this->view->assign("xishu",$xishu);	//传系数过去
	        $this->view->display("activity/module/edit", array('row' => $row));	//传数据 过去
        
        }
    }

    function del($id) {
    	
        $c = 0;
        $r = '';
 		//echo " id=$id ";
        if ($this->db->delete('act_module', array('tableenum' => $id))) {
            insert_log(translate('MENU_ACTIVITY_MODULE'),translate('DEL'), $autoid);
            $c = 1;
  
            //策略表中也需要删除
            $this->db->delete('act_group_strategy',array('tableenum'=>$id));
            $r = translate('C_TIPS_SUCCESS_TEXT');
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
        
    }

    function multi() {
    	/*
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            if ($type == 'delete') {
                //$this->db->query("DELETE FROM {pre}role WHERE id IN ({$ids})");
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
        */
    }
}